// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   register_draft_ok.java

package stock.pay;

import include.get_rate_from_ID.getRateFromID;
import include.nseer_cookie.FileKind;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import stock.getLength;
import validata.ValidataNumber;

public class register_draft_ok extends HttpServlet
{

	public register_draft_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession;
		nseer_db_backup1 stock_db;
		ValidataNumber validata;
		getLength length;
		dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		HttpSession session = request.getSession();
		stock_db = new nseer_db_backup1(dbApplication);
		validata = new ValidataNumber();
		length = new getLength();
		getRateFromID getRateFromID = new getRateFromID();
		FileKind FileKind = new FileKind();
		String pay_ID;
		String stock_name[];
		String amount[];
		String serial_number_group[];
		String amount_number_group[];
		String serial_number[];
		String product_ID[];
		String product_name[];
		String cost_price[];
		String demand_amount[];
		String paid_amount[];
		String paid_subtotal[];
		String stock_ID[];
		int lll;
		int p;
		int i;
		if (!stock_db.conn((String)dbSession.getAttribute("unit_db_name")))
			break MISSING_BLOCK_LABEL_1759;
		pay_ID = request.getParameter("pay_ID");
		Date now = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = formatter.format(now);
		stock_name = request.getParameterValues("stock_name");
		amount = request.getParameterValues("amount");
		serial_number_group = request.getParameterValues("serial_number");
		amount_number_group = request.getParameterValues("amount_number");
		serial_number = new String[stock_name.length];
		product_ID = request.getParameterValues("product_ID");
		product_name = request.getParameterValues("product_name");
		cost_price = request.getParameterValues("cost_price");
		demand_amount = request.getParameterValues("demand_amount");
		paid_amount = request.getParameterValues("paid_amount1");
		paid_subtotal = request.getParameterValues("paid_subtotal");
		stock_ID = request.getParameterValues("stock_ID");
		lll = 0;
		p = 0;
		i = 0;
		  goto _L1
_L6:
		int serial_number_tag;
		int j;
		if (!amount[i].equals(""))
		{
			if (!validata.validata(amount[i]))
				p++;
		} else
		{
			amount[i] = "0";
		}
		serial_number_tag = 0;
		String sql6 = (new StringBuilder("select * from stock_cell where product_ID='")).append(product_ID[i]).append("' and check_tag='1'").toString();
		ResultSet rs6 = stock_db.executeQuery(sql6);
		if (rs6.next())
			serial_number_tag = rs6.getInt("serial_number_tag");
		if (serial_number_tag == 1)
			lll = length.getLength((String)dbSession.getAttribute("unit_db_name"));
		else
			lll = length.getLength2((String)dbSession.getAttribute("unit_db_name"));
		serial_number[i] = "";
		if (serial_number_tag != 0 && Double.parseDouble(amount[i]) != 0.0D && serial_number_group[i].equals(""))
			p++;
		else
		if (serial_number_tag == 0 && Double.parseDouble(amount[i]) != 0.0D && !serial_number_group[i].equals(""))
			p++;
		if (serial_number_group[i].equals(""))
			continue; /* Loop/switch isn't completed */
		if (serial_number_group[i].length() % lll == 0)
		{
			if (serial_number_tag == 1 && (double)(serial_number_group[i].length() / lll) != Double.parseDouble(amount[i]))
				p++;
		} else
		{
			p++;
		}
		if (p != 0)
			continue; /* Loop/switch isn't completed */
		j = 0;
		  goto _L2
_L4:
		serial_number;
		i;
		JVM INSTR dup2 ;
		JVM INSTR aaload ;
		JVM INSTR new #129 <Class StringBuilder>;
		JVM INSTR dup_x1 ;
		JVM INSTR swap ;
		String.valueOf();
		StringBuilder();
		serial_number_group[i].substring(j, j + lll);
		append();
		", ";
		append();
		toString();
		JVM INSTR aastore ;
		String sql3 = (new StringBuilder("select * from stock_balance_details_details where serial_number='")).append(serial_number_group[i].substring(j, j + lll)).append("' and stock_ID='").append(stock_ID[i]).append("' and product_ID='").append(product_ID[i]).append("'").toString();
		ResultSet rs3 = stock_db.executeQuery(sql3);
		if (!rs3.next())
			p++;
		if (serial_number_tag == 1)
		{
			String sql = (new StringBuilder("insert into stock_serial_number_temp(serial_number) values('")).append(serial_number_group[i].substring(j, j + lll)).append("')").toString();
			stock_db.executeUpdate(sql);
		}
		j += lll;
_L2:
		if (j < serial_number_group[i].length()) goto _L4; else goto _L3
_L3:
		if (serial_number_tag == 1)
		{
			String sql1 = "select distinct serial_number from stock_serial_number_temp";
			ResultSet rs1 = stock_db.executeQuery(sql1);
			rs1.last();
			if ((double)rs1.getRow() != Double.parseDouble(amount[i]))
				p++;
			String sql2 = "delete from stock_serial_number_temp";
			stock_db.executeUpdate(sql2);
		}
		i++;
_L1:
		if (i < stock_name.length) goto _L6; else goto _L5
_L5:
		if (p == 0)
		{
			String register = request.getParameter("register");
			String register_time = request.getParameter("register_time");
			String reason = request.getParameter("reason");
			String reasonexact = request.getParameter("reasonexact");
			String nick_name[] = request.getParameterValues("nick_name");
			String details_number[] = request.getParameterValues("details_number");
			String available_amount[] = request.getParameterValues("available_amount");
			int n = 0;
			double amount_sum = 0.0D;
			for (int i = 0; i < stock_name.length; i++)
				if (!amount[i].equals(""))
				{
					amount_sum += Double.parseDouble(amount[i]);
					if (Double.parseDouble(amount[i]) > Double.parseDouble(available_amount[i]))
						n++;
					else
					if (Math.abs(Double.parseDouble(amount[i]) + Double.parseDouble(paid_amount[i])) > Math.abs(Double.parseDouble(demand_amount[i])))
						n++;
				}

			if (n == 0)
			{
				int paying_time = 0;
				String sqq = (new StringBuilder("select paying_time from stock_pay where pay_ID='")).append(pay_ID).append("'").toString();
				ResultSet rsq = stock_db.executeQuery(sqq);
				if (rsq.next())
					paying_time = rsq.getInt("paying_time") + 1;
				sqq = (new StringBuilder("update stock_pay set paying_time='")).append(paying_time).append("',check_tag='5' where pay_ID='").append(pay_ID).append("'").toString();
				stock_db.executeUpdate(sqq);
				try
				{
					double amount4 = 0.0D;
					double cost_price_sum = 0.0D;
					for (int i = 0; i < stock_name.length; i++)
					{
						int serial_number_tag = 0;
						String sql6 = (new StringBuilder("select * from stock_cell where product_ID='")).append(product_ID[i]).append("' and check_tag='1'").toString();
						ResultSet rs6 = stock_db.executeQuery(sql6);
						if (rs6.next())
							serial_number_tag = rs6.getInt("serial_number_tag");
						if (serial_number_tag == 1)
							lll = length.getLength((String)dbSession.getAttribute("unit_db_name"));
						else
							lll = length.getLength2((String)dbSession.getAttribute("unit_db_name"));
						if (!amount[i].equals("") && Double.parseDouble(amount[i]) != 0.0D)
						{
							double subtotal = Double.parseDouble(cost_price[i]) * Double.parseDouble(amount[i]);
							amount_number_group[i] = (new StringBuilder(String.valueOf(amount[i]))).append(", ").toString();
							double amount22 = Double.parseDouble(amount[i]) + Double.parseDouble(paid_amount[i]);
							String sql4 = (new StringBuilder("insert into stock_paying_gathering(pay_ID,product_ID,details_number,product_name,amount,demand_amount,paid_amount,cost_price,subtotal,paid_subtotal,register,register_time,stock_ID,stock_name,nick_name,serial_number,amount_number,check_tag,paying_time) values('")).append(pay_ID).append("','").append(product_ID[i]).append("','").append(details_number[i]).append("','").append(product_name[i]).append("','").append(amount[i]).append("','").append(demand_amount[i]).append("','").append(paid_amount[i]).append("','").append(cost_price[i]).append("','").append(subtotal).append("','").append(paid_subtotal[i]).append("','").append(register).append("','").append(register_time).append("','").append(stock_ID[i]).append("','").append(stock_name[i]).append("','").append(nick_name[i]).append("','").append(serial_number[i]).append("','").append(amount_number_group[i]).append("','0','").append(paying_time).append("')").toString();
							stock_db.executeUpdate(sql4);
							sql6 = (new StringBuilder("delete from stock_paying_temp where pay_ID='")).append(pay_ID).append("' and product_ID='").append(product_ID[i]).append("'").toString();
							stock_db.executeUpdate(sql6);
							String sql = (new StringBuilder("update stock_pre_paying set pay_check_tag='5' where pay_ID='")).append(pay_ID).append("' and product_ID='").append(product_ID[i]).append("' and stock_ID='").append(stock_ID[i]).append("'").toString();
							stock_db.executeUpdate(sql);
						}
					}

					response.sendRedirect("stock/pay/register_draft_ok.jsp?finished_tag=0");
				}
				catch (Exception ex)
				{
					ex.printStackTrace();
				}
			} else
			{
				response.sendRedirect("stock/pay/register_draft_ok.jsp?finished_tag=1");
			}
		} else
		{
			response.sendRedirect("stock/pay/register_draft_ok.jsp?finished_tag=2");
		}
		stock_db.commit();
		stock_db.close();
		break MISSING_BLOCK_LABEL_1778;
		response.sendRedirect("error_conn.htm");
		break MISSING_BLOCK_LABEL_1778;
		Exception ex;
		ex;
		ex.printStackTrace();
	}
}
